package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ry.novel.model.dto.UserInfo;
import com.ry.novel.model.entity.User;
import com.ry.novel.enums.NovelConst;
import com.ry.novel.enums.ResponseStatus;
import com.ry.novel.exception.BusinessException;
import com.ry.novel.mapper.UserMapper;
import com.ry.novel.service.UserService;
import com.ry.novel.utils.DateUtil;
import com.ry.novel.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author ry
 * @since 2022-01-22 14:51
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo register(UserInfo userInfo) {
        //查询当前用户名是否已经被注册了
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userInfo.getUsername());
        Integer count = userMapper.selectCount(queryWrapper);
        if (count >= 1){
            throw new BusinessException(ResponseStatus.USERNAME_EXIST);
        }
        User entity = new User();
        entity.setUsername(userInfo.getUsername());
        String rawPass = userInfo.getPassword();
        String encodePass = MD5Util.MD5Encode(rawPass, Charset.forName("utf8").name());
        entity.setPassword(encodePass);
        entity.setNickname(userInfo.getUsername());
        entity.setUserPhoto("http://localhost:8081/myFile/1650813308032user-fill.png");
        entity.setGender(userInfo.getGender());
        entity.setState(NovelConst.ACCOUNT_STATE_NORMAL);
        entity.setCreateTime(LocalDateTime.now().toString());
        userMapper.insert(entity);

        userInfo.setId(entity.getId());
        userInfo.setNickname(userInfo.getUsername());
        userInfo.setCreateTime(LocalDateTime.now().toString());
        userInfo.setPassword(null);
        userInfo.setState(NovelConst.ACCOUNT_STATE_NORMAL);

        return userInfo;
    }

    /**
     * 登录方法
     *
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo login(UserInfo userInfo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userInfo.getUsername());
        queryWrapper.eq("password",MD5Util.MD5Encode(userInfo.getPassword()));
        User entity = userMapper.selectOne(queryWrapper);
        if (null == entity){
            //没有对应的用户
            throw new BusinessException(ResponseStatus.USERNAME_PASSWORD_ERROR);
        }
        if(entity.getState().equals(NovelConst.ACCOUNT_STATE_FORBIDDEN)){
            throw new BusinessException(ResponseStatus.USER_STATE_FORBIDDEN);
        }
        UserInfo info = getUserInfo(entity);


        return info;
    }

    @Override
    public UserInfo queryUserById(Long id) {
        User entity = userMapper.selectById(id);
        UserInfo userInfo = getUserInfo(entity);


        return userInfo;
    }

    private UserInfo getUserInfo(User entity) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(entity.getId());
        userInfo.setUsername(entity.getUsername());
        userInfo.setNickname(entity.getNickname());
        userInfo.setUserPhoto(entity.getUserPhoto());
        userInfo.setGender(entity.getGender());
        userInfo.setState(entity.getState());
        userInfo.setCreateTime(DateUtil.format(entity.getCreateTime()));

        return userInfo;
    }
}
